package com.erpanderp.wam.usm.view.user;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.erpanderp.wam.usm.model.entity.leave.EmployeeLeave;
import com.erpanderp.wam.usm.model.entity.user.EmployeeAndReporting;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.leave.EmployeeLeaveDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeAndReportingDAO;
import com.erpanderp.wam.util.CommonLookup;

@ManagedBean
@ViewScoped
public class TeamCalendarBean implements Serializable{

	private static final long serialVersionUID = 1L;
	private HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	
	private WAMUser wamUser;
	private String dateNow;
	
	private List<EmployeeAndReporting> competencyEmployeeList = new ArrayList<EmployeeAndReporting>();
	private List<EmployeeLeave> competencyLeavesList = new ArrayList<EmployeeLeave>();
	
	private EmployeeAndReportingDAO employeeAndReportingDAO=new EmployeeAndReportingDAO();
	private EmployeeLeaveDAO employeeLeaveDAO =  new EmployeeLeaveDAO();
	
	DateFormat formatter =new SimpleDateFormat("yyyy-MM-dd"); 
	Date date ; 
	private int sno=1;

	public TeamCalendarBean() {
		wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
		
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formate= new SimpleDateFormat("yyyy-MM-dd");
		dateNow = formate.format(currentDate.getTime());
		System.out.println("dateNow============"+dateNow);		
	    try {
			date = (Date)formatter.parse(dateNow);
		} catch (ParseException e) {
			e.printStackTrace();
		} 
		competencyEmployeeList = employeeAndReportingDAO.getAllEmployeesByCompetency(wamUser.getEmployee().getEmployeeCode());
		
		for(EmployeeAndReporting employeeAndReporting : competencyEmployeeList){
			employeeAndReporting.setsNo(sno);
			sno++;
		}
		competencyLeavesList = employeeLeaveDAO.getAllLeavesByCompetency(wamUser.getEmployee().getEmployeeCode(),date);
		
	}
    //GET EMPLYEE LEAVES
	public List<EmployeeLeave> getEmployeeLeavesAction() {
		
		try{
			System.out.println("SELECT DATE ::::::::"+getDate());
			competencyLeavesList = employeeLeaveDAO.getAllLeavesByCompetency(wamUser.getEmployee().getEmployeeCode(),getDate());
			
			//FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "Skill Matrix updated successfully.", ""));
		}catch (Exception e) {
			e.printStackTrace();
			//FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed to update Skill Matrix.", e.getMessage()));
		}
	
		return competencyLeavesList;
	}
	
	//SETTERS AND GETTERS
	public List<EmployeeAndReporting> getCompetencyEmployeeList() {
		return competencyEmployeeList;
	}

	public void setCompetencyEmployeeList(List<EmployeeAndReporting> competencyEmployeeList) {
		this.competencyEmployeeList = competencyEmployeeList;
	}

	public EmployeeAndReportingDAO getEmployeeAndReportingDAO() {
		return employeeAndReportingDAO;
	}

	public void setEmployeeAndReportingDAO(EmployeeAndReportingDAO employeeAndReportingDAO) {
		this.employeeAndReportingDAO = employeeAndReportingDAO;
	}

	public String getDateNow() {
		return dateNow;
	}

	public void setDateNow(String dateNow) {
		this.dateNow = dateNow;
	}

	public List<EmployeeLeave> getCompetencyLeavesList() {
		return competencyLeavesList;
	}

	public void setCompetencyLeavesList(List<EmployeeLeave> competencyLeavesList) {
		this.competencyLeavesList = competencyLeavesList;
	}
	public EmployeeLeaveDAO getEmployeeLeaveDAO() {
		return employeeLeaveDAO;
	}
	public void setEmployeeLeaveDAO(EmployeeLeaveDAO employeeLeaveDAO) {
		this.employeeLeaveDAO = employeeLeaveDAO;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public int getSno() {
		return sno;
	}
	public void setSno(int sno) {
		this.sno = sno;
	}

}
